var observer = new MutationObserver(function (mutations) {
    mutations.forEach(function (mutation) {
        if (mutation.type === 'childList' && mutation.addedNodes[0]) {
            if (mutation.addedNodes[0].className === 'ld-radio-group') { // 监听元素的创建
                const inputs = mutation.addedNodes[0].querySelectorAll("input")
                for (let i = 0; i < inputs.length; i++) {
                    renderRadio(inputs[i], inputs[i].parentNode, inputs[i].value, inputs[i].getAttribute("label"), inputs[i].getAttribute("name"), inputs[i].getAttribute("checked"), inputs[i].getAttribute("disabled"))
                }
            }
        }
    });
});

observer.observe(window.document, {
    subtree: true,
    childList: true,
    attributes: true,
    attributeFilter: ['src', 'href', 'action']
});
const ldRadioTags = document.querySelectorAll(".ld-radio")
for (let i = 0; i < ldRadioTags.length; i++) {
    renderRadio(ldRadioTags[i], ldRadioTags[i].parentNode, ldRadioTags[i].value, ldRadioTags[i].getAttribute("label"), ldRadioTags[i].getAttribute("name"), ldRadioTags[i].getAttribute("checked"), ldRadioTags[i].getAttribute("disabled"))
    ldRadioTags[i].parentNode.removeChild(ldRadioTags[i])
}
function renderRadio(el, elparent, value, label, name, checked, disabled) {
    let disabledStr = ""
    let checkActive = "_i_ld-radio_active"
    // 只读判断 
    if (disabled === "true" || disabled === "" || disabled === "disabled") {
        disabledStr = "disabled"
    } else {
        disabledStr = ""
    }
    // 默认选中判断
    if (checked === "true" || checked === "" || checked === "checked") {
        setTimeout(() => {
            $(newRadio).find(".ld-radio").prop("checked", true)
        }, 10);
    } else {
        checkActive = ""
        checked = false
    }
    const str = `<span class="_span_ld-init"><input ${disabledStr} class="ld-radio" type="radio" name="${name}" value="${value}">${label}</span>
                    <i class="_i_ld-radio ${checkActive}" name="${name}"></i>
                    <span class="_span_ld-radio">${label}</span>`
    const newRadio = document.createElement("label")
    // 只读判断 
    if (disabled === "true" || disabled === "" || disabled === "disabled") {
        disabledStr = "disabled"
        newRadio.className = "_span_ld-init_label disabled"
    } else {
        disabledStr = ""
        newRadio.className = "_span_ld-init_label"
    }

    newRadio.innerHTML = str
    elparent.insertBefore(newRadio, el)
    // 默认选中判断
    if (checked === "true" || checked === "" || checked === "checked") {
        newRadio.querySelector(".ld-radio").setAttribute("checked", true)
        setTimeout(() => {
            $(newRadio).find(".ld-radio").prop("checked", true)
        }, 10);
    } else {
        checkActive = ""
        checked = false
        newRadio.querySelector(".ld-radio").setAttribute("checked", false)
    }
    // 绑定点击事件
    newRadio.addEventListener("click", function () {
        if (this.className.indexOf("disabled") == -1) {
            for (let i = 0; i < this.parentNode.querySelectorAll("label").length; i++) {
                this.parentNode.querySelectorAll("label")[i].querySelector("._i_ld-radio").className = "_i_ld-radio"
                this.parentNode.querySelectorAll("label")[i].querySelector(".ld-radio").setAttribute("checked", false)
            }
            this.querySelector("._i_ld-radio").className = "_i_ld-radio _i_ld-radio_active"
            this.querySelector(".ld-radio").setAttribute("checked", true)
        }

    }, false)
}

